﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sharp.Domain.Model;
using Sharp.Domain.PlatformBoundedContext.ConfigModule;
using Sharp.Domain.PlatformBoundedContext.PermissionModule.Aggregates;
using Sharp.Domain.PlatformBoundedContext.UserModule.Aggregates;

namespace Sharp.Domain.PlatformBoundedContext.Migrations
{
    public class InitData
    {
        public void Fill(Entities context = null)
        {
            if (context == null)
                context = new Entities();
            try
            {
                #region 初始化角色

                if (!context.Set<Role>().Any(x => x.Name == "超级管理员"))
                {
                    context.Set<Role>().Add(
                    new Role
                    {
                        Name = "超级管理员",
                        Permissions = context.Set<Permission>().Where(x => x.Code.StartsWith("001") || x.Code.StartsWith("002") || x.Code.StartsWith("003")).ToList(),
                        Remark = "用来维护系统的管理账号。",
                        EntityState = EntityStateOperate.Normal
                    });
                    context.Set<Role>().Add(
                    new Role
                    {
                        Name = "用户",
                        Remark = "非管理员的一般用户。",
                        EntityState = EntityStateOperate.Normal
                    });
                    context.SaveChanges();
                }
                #endregion
                #region 初始化用户
                if (!context.Set<User>().Any(x => x.UserName == "Admin"))
                {
                    context.Set<User>().AddOrUpdate(
                        p => p.UserName,
                        new User()
                        {
                            UserName = "Admin",
                            Password = "123".Sha1(),
                            Profile = new UserProfile()
                            {
                                LastPasswordChangedDate = null,
                                RegisteredTime = DateTime.Now,
                                IsApproved = true,
                                IsLockedOut = false
                            },
                            LastLogonTime = null,
                            EntityState = EntityStateOperate.Normal
                        });
                    context.SaveChanges();

                    var system = context.Set<User>().First(x => x.UserName == "Admin");
                    system.Roles.Clear();
                    system.Permissions.Clear();
                    system.Roles = context.Set<Role>().Where(x => x.Name == "超级管理员").ToList();
                    system.Permissions = context.Set<Permission>().ToList();
                    context.SaveChanges();
                }

                #endregion
                #region    初始化 OAuth数据
                if (!context.Set<OAuthConfig>().Any(x => x.Interface == OAuthConfig.OAuthType.Platform))
                {
                    context.Set<OAuthConfig>().AddOrUpdate(x => x.Interface, new OAuthConfig()
                    {
                        AppId = "1234",
                        AppSecret = "5678",
                        Interface = OAuthConfig.OAuthType.Platform,
                        IsEnable = true
                    });
                }
                #endregion
                context.SaveChanges();
            }
            catch (DbEntityValidationException)
            {
                throw;
            }
            finally
            {
                context.Dispose();
            }
        }
    }
}
